package bean;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.renderable.ParameterBlock;
import java.io.File;
import java.sql.Connection;
import java.util.Enumeration;

import javax.imageio.ImageIO;
import javax.media.jai.JAI;
import javax.media.jai.RenderedOp;

import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;

public class BoardDTO {
	private BoardVO vo = null;
	private Connection conn = null;
	
	public BoardDTO(){
		conn = new MyOracle().getConn();
	}
	
	public BoardVO getVo() {
		return vo;
	}

	public void setVo(BoardVO vo) {
		this.vo = vo;
	}

	public void setVO(BoardVO vo) {
		this.vo = vo; // multipartrequest를 여기서 생성한다.
		int size = 1024 * 1024 * 10;
		String saveDir = "C:/Javaworkspace/jsp1024_1/WebContent/memberPhoto";
		String sysFile = "";
		MultipartRequest multi = null;

		try {
			if (vo.getRequest() != null) {
				multi = new MultipartRequest(vo.getRequest(), saveDir, size,
						"utf-8", new DefaultFileRenamePolicy());

				Enumeration files = multi.getFileNames();
				String tempFile = (String) files.nextElement();

				sysFile = multi.getOriginalFileName(tempFile);

				vo.setM_id(multi.getParameter("mid"));
			
				ParameterBlock pb= new ParameterBlock();
				pb.add(saveDir + "/" + sysFile);
				RenderedOp rOp = JAI.create("fileload", pb);
				
				BufferedImage bi = rOp.getAsBufferedImage();
				BufferedImage thumb = new BufferedImage(100, 100,BufferedImage.TYPE_INT_RGB);
				Graphics2D g=thumb.createGraphics();
				g.drawImage(bi,0,0,100,100,null);
				
				File file = new File(saveDir + "/" + "sm_" + sysFile);
				ImageIO.write(thumb, "png", file);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
}
